package a51qindian.com.comprehensive.fragment;

import android.net.Uri;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;

import java.util.HashMap;
import java.util.Map;

import a51qindian.com.comprehensive.R;
import a51qindian.com.comprehensive.base.QDBaseFragment;
import a51qindian.com.comprehensive.model.BaseModel;
import a51qindian.com.comprehensive.model.UserDetailInfo;
import a51qindian.com.comprehensive.net.ObservableHelper;
import a51qindian.com.comprehensive.net.RetrofitCreator;
import a51qindian.com.comprehensive.net.SelectorObserver;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.rong.imkit.RongIM;
import io.rong.imkit.fragment.ConversationListFragment;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;

/**
 * Author: Li
 * Date: 2017/9/12 0012
 * Description: 消息界面
 */

public class MsgFragment extends QDBaseFragment {

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_city_msg;
    }
    @Override
    protected void initSth()  {
        super.initSth();
        getUserInfo(mUid,"android");

    }
    private void getUserInfo(String uid, String request) {
        Map<String, String> map = new HashMap<>();
        map.put("uid", uid);
        map.put("request", request);
        ObservableHelper.createObservable(RetrofitCreator.createService().userDetailInfo(map))
                .subscribe(new SelectorObserver<BaseModel<UserDetailInfo>>() {
                    @Override
                    protected void onErrorCodeInfo(int code, String info) {

                    }

                    @Override
                    protected void onSuccess(BaseModel<UserDetailInfo> userDetailInfoBaseModel) {
                        connect(userDetailInfoBaseModel.getData().getToken());

                    }

                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                    }
                });
    }
    private void connect(String token) {
        RongIM.connect(token, new RongIMClient.ConnectCallback() {
            @Override
            public void onTokenIncorrect() {
                Log.e("tag", "onTokenIncorrect: ");
            }

            @Override
            public void onSuccess(String s) {
                ConversationListFragment conversationListFragment = new ConversationListFragment();
                Uri uri = Uri.parse("rong://" + mContext.getApplicationInfo().packageName)
                        .buildUpon()
                        .appendPath("conversationlist")
                        .appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false")
                        .appendQueryParameter(Conversation.ConversationType.SYSTEM.getName(), "true")
                        .build();
                conversationListFragment.setUri(uri);


                FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                transaction.add(R.id.rong_container, conversationListFragment);
                transaction.commitAllowingStateLoss();
            }

            @Override
            public void onError(RongIMClient.ErrorCode errorCode) {
                Log.e("tag", "onError: "+errorCode);
            }
        });
    }

}
